

#include "lvgl.h"
#include "pt_printk.h"
#include "root.h"
#include "sys_msg.h"

#include "mcntl_cjson.h"

#include "midea_collection_view.h"
#include "midea_collection_view_single.h"

const char *the_collection_name = "midea_test_collection";


static lv_obj_t * u_test1_obj = NULL;
static lv_obj_t * u_test2_obj = NULL;
static lv_obj_t * u_test3_obj = NULL;
static lv_obj_t * u_test4_obj = NULL;

static uint8_t r = 70;
static uint8_t g = 140;
static uint8_t b = 210;

static void test1_time_callback(struct _lv_timer_t * t)
{
	r++;
	g++;
	b++;

	// //lv_obj_set_style_bg_opa(u_test1_obj, LV_OPA_100, LV_PART_MAIN);
	//lv_obj_set_style_bg_color(u_test1_obj, lv_color_make(r,g,b), LV_PART_MAIN);
}

void ui_test3(void)
{
    u_test3_obj  = lv_obj_create(lv_scr_act());

    //lv_obj_set_style_radius(u_test3_obj, 0, LV_PART_MAIN);
	//lv_obj_set_style_border_width(u_test3_obj, 0, LV_PART_MAIN);
    //lv_obj_set_style_border_opa(u_test3_obj, LV_OPA_0, LV_PART_MAIN);
	//lv_obj_set_style_border_color(u_test3_obj, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
    //lv_obj_set_style_pad_all(u_test3_obj,0,0);

    #if 0
	//lv_obj_set_style_bg_opa(u_test3_obj, LV_OPA_0, LV_PART_MAIN);
    #else
	// //lv_obj_set_style_bg_opa(u_test3_obj, LV_OPA_100, LV_PART_MAIN);
	// //lv_obj_set_style_bg_color(u_test3_obj, lv_color_make(0,0,0xFF), LV_PART_MAIN);
    #endif

	lv_obj_set_pos(u_test3_obj,0,320 - 100);
	lv_obj_set_size(u_test3_obj,100,100);

}

void ui_test4(void)
{
    u_test4_obj  = lv_obj_create(lv_scr_act());

    //lv_obj_set_style_radius(u_test4_obj, 0, LV_PART_MAIN);
	//lv_obj_set_style_border_width(u_test4_obj, 0, LV_PART_MAIN);
    //lv_obj_set_style_border_opa(u_test4_obj, LV_OPA_0, LV_PART_MAIN);
	//lv_obj_set_style_border_color(u_test4_obj, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
    //lv_obj_set_style_pad_all(u_test4_obj,0,0);

    #if 0
	//lv_obj_set_style_bg_opa(u_test4_obj, LV_OPA_0, LV_PART_MAIN);
    #else
	// //lv_obj_set_style_bg_opa(u_test4_obj, LV_OPA_100, LV_PART_MAIN);
	// //lv_obj_set_style_bg_color(u_test4_obj, lv_color_make(0xFF,0xff,0), LV_PART_MAIN);
    #endif

	lv_obj_set_pos(u_test4_obj,480 - 100,320 - 100);
	lv_obj_set_size(u_test4_obj,100,100);

}



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void update_collection(void *data, lv_obj_t *item_view, int32_t index, uint8_t ui_type)
{
    if (data == NULL || item_view == NULL)
        return;

    lv_obj_t *obj = item_view;
    if (ui_type == 1)
    {
        lv_obj_set_style_bg_color(obj,lv_color_make(0xff,0xff,0xff), 0);
        lv_obj_set_size(obj, 480, 480);
    }
    else if (ui_type == 2)
    {
        lv_obj_set_size(obj, 480, 480);
        lv_obj_set_style_bg_color(obj,lv_color_make(0x00,0xff,0x00), 0);
    }
    else 
    {
        lv_obj_set_size(obj, 480, 480);
        lv_obj_set_style_bg_color(obj,lv_color_hex(0xe8380d), 0);
    }


	//debug_d("update ui_type=%d\n",ui_type);
#if 0
    cloud_dev_data_t *dev_data = (cloud_dev_data_t *)data;
    switch (ui_type)
    {
    case COMMON_UI_LIGHT:
    {
        light_card_update(item_view, dev_data);
        break;
    }
    case COMMON_UI_LIGHT_WITH_DOUBLE_SLIDER:
    {
        light_card_with_double_slider_update(item_view, dev_data);
        break;
    }
    case COMMON_UI_CURTAIN:
    {
        curtain_card_update(item_view, dev_data);
        break;
    }
    case COMMON_UI_BTN_SET:
        break;
    default:
        break;
    }
#endif
}

static void install_collection(void *data, lv_obj_t *item_view, int32_t index, uint8_t ui_type)
{
    lv_obj_t *obj = item_view;
    if (ui_type == 0)
    {
        lv_obj_set_style_bg_color(obj,lv_color_make(0xff,0xff,0xff), 0);
        lv_obj_set_size(obj, 480, 480);
    }
    else if (ui_type == 99)
    {
        lv_obj_set_size(obj, 480, 480);
        lv_obj_set_style_bg_color(obj,lv_color_make(0x00,0xff,0x00), 0);
    }
    else 
    {
        lv_obj_set_size(obj, 480, 480);
        lv_obj_set_style_bg_color(obj,lv_color_hex(0xe8380d), 0);
    }
    // else 
    // {
    printf("huocy ui_type = %u\n",ui_type);
    //     lv_obj_set_size(obj, 100, 480);
    //     lv_obj_set_style_bg_color(obj,lv_color_make(0x00,0x00,0xff), 0);
    // }
    lv_obj_set_style_bg_opa(obj, LV_OPA_0, LV_PART_SCROLLBAR | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(obj, LV_OPA_0, LV_PART_SCROLLBAR | LV_STATE_SCROLLED);
    lv_obj_set_style_border_color(obj, lv_color_hex(0xe8e8e8), LV_STATE_DEFAULT);
    // lv_obj_set_style_border_opa(obj, LV_OPA_0, LV_STATE_DEFAULT);
    lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLLABLE);

	// switch (ui_type){
	// case 0:{
    //     lv_obj_set_size(item_view,480,180);
    //     lv_obj_set_style_bg_color(item_view,lv_color_make(0x00,0x00,0xff), 0);
	// 	// //lv_obj_set_style_bg_opa(item_view,LV_OPA_100, 0);
	// 	// lv_obj_set_style_bg_color(cell,lv_color_red(), 0);
	
	// 	//lv_obj_set_size(item_view, 480-20, 320-20);

	// 	break;
	// }
	// case 1:{
    //     lv_obj_set_size(item_view,480,100);
    //     // lv_obj_set_style_bg_color(cell,lv_color_make(0x00,0xff,0x00), 0);
	// 	// //lv_obj_set_style_bg_opa(item_view,LV_OPA_100, 0);
	// 	// lv_obj_set_style_bg_color(cell,lv_color_green(), 0);


	// 	break;
	// }
	// case 2:{
	// 	// //lv_obj_set_style_bg_opa(item_view,LV_OPA_100, 0);
	// 	// lv_obj_set_style_bg_color(cell,lv_color_make(0xff,0x00,0x00), 0);

	// 	lv_obj_set_size(item_view, 480-20, 320-20);

    //     lv_obj_t* label = lv_label_create(item_view);
    //     lv_obj_set_pos(label,20,20);
    //     lv_obj_set_size(label,100,100);
    //     // //lv_obj_set_style_bg_opa(label,LV_OPA_100, 0);
    //     lv_obj_set_style_bg_color(label,lv_color_make(0xff,0x00,0xff), 0);
    //      lv_label_set_text( label,"3333333333333333333333");

	// 	break;
	// }
	// default:break;
	// }

#if 0
    lv_obj_t *obj = item_view;
    //lv_obj_set_style_radius(obj, 32, 0);
    //lv_obj_set_style_bg_opa(obj, LV_OPA_0, 0);
    cloud_dev_data_t *dev_data = (cloud_dev_data_t *)data;
    switch (ui_type)
    {
    case COMMON_UI_LIGHT:
    {
        lv_obj_t *module = light_card(item_view, dev_data);
        break;
    }
    case COMMON_UI_LIGHT_WITH_DOUBLE_SLIDER:
    {
        lv_obj_t *module = light_card_with_double_slider(item_view, dev_data);
        break;
    }
    case COMMON_UI_CURTAIN:
    {
        lv_obj_t *module = curtain_card(item_view, dev_data);
        break;
    }
    case COMMON_UI_BTN_SET:
    {
        lv_obj_t *module = m_panel_bg_create(item_view, lv_color_hex(0x142C5C));
        //lv_obj_set_style_bg_opa(module, LV_OPA_0, 0);
        lv_obj_set_size(module, 432, 64);
        lv_obj_t *btn = lv_midea_button_create(module, "设置常用场景和设备");
        lv_obj_set_size(btn, 432, 64);
        lv_obj_set_pos(btn, 0, 0);
        lv_font_t *font = lv_midea_font_create_by_ft(LV_MIDEA_FONT_TYPE_MIDEA, LV_MIDEA_FONT_STYLE_REGULAR, 24);
        //lv_obj_set_style_text_color(btn, lv_color_hex(0xB35336), LV_STATE_DEFAULT);
        //lv_obj_set_style_text_font(btn, font, LV_PART_MAIN);
        //lv_obj_set_style_bg_color(btn, lv_color_hex(0x27272A), LV_STATE_DEFAULT);
        //lv_obj_set_style_bg_opa(btn, LV_OPA_100, LV_STATE_DEFAULT);
        lv_obj_add_event_cb(btn, common_set_btn_cb, LV_EVENT_CLICKED, NULL);
        break;
    }
    case COMMON_UI_LABEL:
    {
        lv_obj_t *module = m_panel_bg_create(item_view, lv_color_hex(0x00000));
        //lv_obj_set_style_bg_opa(module, LV_OPA_0, 0);
        lv_obj_set_size(module, 432, 266);

        lv_font_t *font = lv_midea_font_create_by_ft(LV_MIDEA_FONT_TYPE_MIDEA, LV_MIDEA_FONT_STYLE_REGULAR, 28);
        lv_obj_t *label_1 = lv_label_create(module);
        //lv_obj_set_style_text_font(label_1, font, LV_PART_MAIN);
        lv_label_set_text(label_1, "暂无常用设备");
        //lv_obj_set_style_text_color(label_1, lv_color_hex(0x9B9B9B), LV_PART_MAIN);
        //lv_obj_set_style_text_opa(label_1, LV_OPA_100, LV_PART_MAIN);
        lv_obj_set_pos(label_1, 132, 134);

        font = lv_midea_font_create_by_ft(LV_MIDEA_FONT_TYPE_MIDEA, LV_MIDEA_FONT_STYLE_REGULAR, 20);
        lv_obj_t *label_2 = lv_label_create(module);
        //lv_obj_set_style_text_font(label_2, font, LV_PART_MAIN);
        lv_label_set_text(label_2, "可前往 “美的美居App” 设置常用场景和设备");
        //lv_obj_set_style_text_color(label_2, lv_color_hex(0x9B9B9B), LV_PART_MAIN);
        //lv_obj_set_style_text_opa(label_2, LV_OPA_100, LV_PART_MAIN);
        lv_obj_align_to(label_2, label_1, LV_ALIGN_OUT_BOTTOM_MID, 0, 32);
        break;
    }
    break;
    default:
        break;
    }
#endif
}

static void free_data(void *data)
{
	// debug_d("free data\n");
	#if 0
    cloud_dev_data_t *tmp = (cloud_dev_data_t *)data;
    if (tmp != NULL)
    {
        if (tmp->js_data != NULL)
        {
            free(tmp->js_data);
            tmp->js_data = NULL;
        }
        if (tmp->applianceCode != NULL)
        {
            free(tmp->applianceCode);
            tmp->applianceCode = NULL;
        }
        if (tmp->dev_name != NULL)
        {
            free(tmp->dev_name);
            tmp->dev_name = NULL;
        }
        if (tmp->room_name != NULL)
        {
            free(tmp->room_name);
            tmp->room_name = NULL;
        }
        free(tmp);
        tmp = NULL;
    }
	#endif
}

#if 0
static void install_collection(void *data, lv_obj_t *item_view, int32_t index, uint8_t ui_type)  创建卡片
static void free_data(void *data) 销毁卡片
static void update_collection(void *data, lv_obj_t *item_view, int32_t index, uint8_t ui_type)  更新卡片
lv_obj_t *collection_content = create_midea_collection_single("common_collection", parent, LV_DIR_VER, update_collection, install_collection, free_data);  LV_DIR_VER 用于控制横向滚动还是 纵向
#endif

typedef struct {
    char *name;
    int32_t id;
}midea_collection_test_data;

static lv_obj_t *collection_content= NULL;

// static void collection_view_scroll_cb(lv_event_t *e)
// {
//     lv_obj_t *obj = lv_event_get_target(e);
//     lv_event_code_t code = lv_event_get_code(e);
//         lv_point_t  lv_point;
//     lv_indev_t* indev = NULL;
//     indev = lv_indev_get_act();
//     if(!indev){
//         return ;
//     }

//     // lv_indev_get_point(indev, &lv_point);

//     // printf("x:y=%d:%d\n",lv_point.x,lv_point.y);
//         lv_coord_t x = lv_obj_get_scroll_x(obj);
//         lv_coord_t y = lv_obj_get_scroll_y(obj);
//     int cnt = lv_obj_get_child_cnt(collection_content);

//      printf("cnt=%d\n",cnt);

//     printf("code=%d:%d:%d\n",LV_EVENT_SCROLL,LV_EVENT_SCROLL_END,code);
//     printf("scrol:x:y=%d:%d\n",x,y);

//     if (code == LV_EVENT_SCROLL_END)
//     {
//         lv_obj_readjust_scroll(obj, LV_ANIM_ON);

//         printf("end:x:y=%d:%d\n",x,y);
//     }
//     else if(code == LV_EVENT_SCROLL)
//     {

//         printf("scrol:x:y=%d:%d\n",x,y);
        
//     }
// }

static void collection_view_scroll_cb(lv_event_t *e)
{
    lv_obj_t *obj = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);

    if (code == LV_EVENT_SCROLL_END)
    {
        printf("UI test code=%d\n",code);
        lv_obj_readjust_scroll(obj, LV_ANIM_ON);
    }
}


void collection_create_init(void)
{

    lv_obj_t *bg_view = lv_obj_create(lv_scr_act());
    lv_obj_remove_style_all(bg_view);
    lv_obj_set_width(bg_view, 480);
    lv_obj_set_height(bg_view, 480);
    lv_obj_set_style_border_width(bg_view, 0, 0);
    lv_obj_set_style_radius(bg_view, 0, 0);
    lv_obj_center(bg_view);
    lv_obj_set_style_bg_color(bg_view, lv_color_hex(0xe8380d), 0);
    lv_obj_set_style_bg_opa(bg_view, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_all(bg_view, 0, 0);
   
    lv_obj_clear_flag(bg_view,LV_OBJ_FLAG_SCROLLABLE);


    lv_coord_t pad = 0;
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_pad_bottom(&style, pad);
    lv_style_set_pad_top(&style, pad);
    lv_style_set_pad_left(&style, 0);
    lv_style_set_pad_right(&style, 0);
    lv_style_set_border_width(&style, 0);
    lv_style_set_bg_color(&style,lv_color_hex(0xff0000));
    lv_style_set_bg_opa(&style,LV_OPA_100);

    collection_content = create_midea_collection_single(the_collection_name, bg_view, LV_DIR_HOR, update_collection,install_collection, free_data);
    lv_obj_add_style(collection_content, &style, LV_STATE_DEFAULT);

    midea_collection_view_single *collection = (midea_collection_view_single*)collection_content;
    collection->dynamic_load_tag.load_tag = MIDEA_DYNAMIC_LOAD;
    lv_obj_align(collection_content,LV_ALIGN_TOP_LEFT,0,0);//colletion组件第一个卡片贴住顶部
    lv_obj_set_style_pad_row(collection_content, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_column(collection_content, 0,LV_PART_MAIN);
    lv_obj_add_flag(collection_content, LV_OBJ_FLAG_SCROLL_ELASTIC | LV_OBJ_FLAG_SCROLL_CHAIN_HOR);
    lv_obj_set_size(collection_content,480,480);


    for (int i = 0; i < 100; i++)
    {
        midea_collection_test_data *data = (midea_collection_test_data *)malloc(sizeof(midea_collection_test_data));
        data->name = (char *)malloc(sizeof(char) * 50);
        data->id = i + 100;
        if (i == 0)
        {
            sprintf(data->name, "AC T628 %d", i);
        }
        else
        {
            sprintf(data->name, "BB M3535 %d", i);
        }

		printf("data=%s\n",data->name);
        // add_midea_collection_single_data(the_collection_name, data, i);
        //  add_midea_collection_single_data(midea_collection_view_single *collection, void *data, uint8_t ui_type);

		add_midea_collection_single_data(collection_content,(void*)data,i);
        
	}

    lv_obj_add_event_cb(collection, collection_view_scroll_cb, LV_EVENT_ALL, NULL);

    // update_midea_collection(the_collection_name);

	update_midea_collection_single_all_item(collection_content);

	// update_midea_collection_single(collection);

}


void test_cjson_array(void)
{
    mcntl_cjson* c_device_array = mcntl_cjson_create_array();

    for(int i=0;i<5;i++)
    {
        char id[28] = {0};
        char name[28] = {0};

        mcntl_cjson* devinfo = mcntl_cjson_create_object();

        snprintf(id,sizeof(id),"id%02d",i);
        snprintf(name,sizeof(name),"name%02d",i);
    
        mcntl_cjson_add_string_to_object(devinfo, "applianceName",id);
        mcntl_cjson_add_string_to_object(devinfo, "applianceId",name);

        mcntl_cjson_add_item_to_array(c_device_array,devinfo);
    }

    char* str_device_array = mcntl_cjson_print(c_device_array);

    mcntl_cjson_delete(c_device_array);
    c_device_array = NULL;

    debug_d("str_device_array=%s\n",str_device_array);

}

char swled_pid[][9]={"10003729","10003740"};
char dwled_pid[][9]={"10003730","10003731","10003732","10003733","10008673","10003741","10003742"};
char mc_pid[][9]={"10000952"};
char oc_pid[][9]={"10004744"};
char jc_pid[][9]={"10006363"};

int find_string_in_array(const char srcstr[][9], int array_size, const char *destr) 
{
    for (int i = 0; i < array_size; i++) {
        if (strcmp(srcstr[i], destr) == 0) {
            return 1; 
        }
    }
    return 0; 
}

void test_spid_add(void)
{
    debug_d("\n");
    if(find_string_in_array((const char **)dwled_pid,sizeof(dwled_pid) / sizeof(dwled_pid[0]),"10003730")){
        debug_d("ok\n");
    }else{
        debug_d("err\n");
    }

}